package com.micro.ai.template.controller;

import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.template.entity.Category;
import com.micro.ai.template.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 分类控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/template/category")
@RequiredArgsConstructor
@Validated
@Tag(name = "分类管理", description = "模板分类的树形结构管理、移动、排序等操作")
public class CategoryController {

    private final CategoryService categoryService;

    /**
     * 创建分类
     */
    @PostMapping
    @Operation(summary = "创建分类", description = "创建新的模板分类")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "创建成功",
            content = @Content(schema = @Schema(implementation = Category.class))),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "参数错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "409", description = "分类名称已存在")
    })
    public ApiResponse<Category> createCategory(@Valid @RequestBody Category category) {
        return categoryService.createCategory(category);
    }

    /**
     * 更新分类
     */
    @PutMapping("/{categoryId}")
    @Operation(summary = "更新分类", description = "更新分类信息")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功",
            content = @Content(schema = @Schema(implementation = Category.class))),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "分类不存在"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "409", description = "分类名称已存在")
    })
    public ApiResponse<Category> updateCategory(
            @Parameter(description = "分类ID") @PathVariable Long categoryId, 
            @Valid @RequestBody Category category) {
        category.setId(categoryId);
        return categoryService.updateCategory(category);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{categoryId}")
    @Operation(summary = "删除分类", description = "删除指定分类（需检查子分类和模板）")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "删除成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "分类不存在"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "409", description = "分类下有子分类或模板")
    })
    public ApiResponse<Void> deleteCategory(
            @Parameter(description = "分类ID") @PathVariable Long categoryId) {
        return categoryService.deleteCategory(categoryId);
    }

    /**
     * 根据ID查询分类
     */
    @GetMapping("/{categoryId}")
    @Operation(summary = "查询分类详情", description = "根据ID查询分类详细信息")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功",
            content = @Content(schema = @Schema(implementation = Category.class))),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "分类不存在")
    })
    public ApiResponse<Category> getCategoryById(
            @Parameter(description = "分类ID") @PathVariable Long categoryId) {
        return categoryService.getCategoryById(categoryId);
    }

    /**
     * 查询分类树
     */
    @GetMapping("/tree")
    @Operation(summary = "查询分类树", description = "查询完整的分类树形结构")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功",
            content = @Content(schema = @Schema(implementation = List.class)))
    })
    public ApiResponse<List<Category>> getCategoryTree() {
        return categoryService.getCategoryTree();
    }

    /**
     * 根据父分类ID查询子分类
     */
    @GetMapping("/parent/{parentId}")
    @Operation(summary = "查询子分类", description = "根据父分类ID查询所有直接子分类")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功",
            content = @Content(schema = @Schema(implementation = List.class)))
    })
    public ApiResponse<List<Category>> getCategoriesByParentId(
            @Parameter(description = "父分类ID") @PathVariable Long parentId) {
        return categoryService.getCategoriesByParentId(parentId);
    }

    /**
     * 移动分类
     */
    @PutMapping("/{categoryId}/move")
    @Operation(summary = "移动分类", description = "将分类移动到新的父分类下")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "移动成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "分类不存在"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "409", description = "不能移动到自己的子分类下")
    })
    public ApiResponse<Void> moveCategory(
            @Parameter(description = "分类ID") @PathVariable Long categoryId, 
            @Parameter(description = "新父分类ID") @RequestParam Long newParentId) {
        return categoryService.moveCategory(categoryId, newParentId);
    }

    /**
     * 更新分类排序
     */
    @PutMapping("/{categoryId}/sort")
    @Operation(summary = "更新分类排序", description = "更新分类的排序值")
    @ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "分类不存在")
    })
    public ApiResponse<Void> updateCategorySort(
            @Parameter(description = "分类ID") @PathVariable Long categoryId, 
            @Parameter(description = "排序值") @RequestParam Integer sortOrder) {
        return categoryService.updateCategorySort(categoryId, sortOrder);
    }
}
